import store from "@/store"
import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../components/HelloWorld.vue"
import Login from "../views/LoginView.vue"
import HomeView from "../views/HomeView.vue"
import CardView from "../views/CardView.vue"
import SelfInfoView from "../views/SelfInfoView.vue"
import ChooseCourse from "../views/ChooseCourse.vue"
import ScoreView from "../views/ScoreView.vue"
import FreeRoomView from "../views/FreeRoomView.vue"
import PasswordModifyView from "@/views/PasswordModifyView.vue";
import SelfInfoEditView from "@/views/SelfInfoEditView.vue";
import CourseView from "@/views/CourseView"


Vue.use(VueRouter);
 
const router = new VueRouter({
  mode: 'hash', // 使用 hash 模式
  routes: [
    {
      path: '/HelloWorld',
      name: 'Home',
      component: Home,
	  
    },
	{
		path: '/',
		name: 'Login',
		component: Login
	},
	{
		path:'/HomeView',
		name:'HomeView',
		component:HomeView,
		meta: {
		        tab: true,            // 标记为标签页
		        tabName: '首页', 
		        tabClosable: false    // 不可关闭
		      }
	},
	{
		path:'/CardView',
		name:'CardView',
		component:CardView,
		meta: {
		        tab: true,            // 标记为标签页
		        tabName: '校园卡流水',       // 固定名称
		        tabClosable: true ,
				requiresTab:true
		      }
	},
	{
		path:'/SelfInfoView',
		name:'SelfInfoView',
		component:SelfInfoView,
		meta: {
		        tab: true,            // 标记为标签页
		        tabName: '个人信息维护',       // 固定名称
		        tabClosable: true ,
				requiresTab:true
		      }
	},
	{
		path:'/SelfInfoEditView',
		name:'SelfInfoEditView',
		component:SelfInfoEditView,
		meta: {
		        tab: true,            // 标记为标签页
		        tabName: '个人信息编辑',       // 固定名称
		        tabClosable: true ,
				requiresTab:true
		      }
	},
	{
		path:'/PasswordModifyView',
		name:'PasswordModifyView',
		component:PasswordModifyView,
		meta: {
		        tab: true,            // 标记为标签页
		        tabName: '密码修改',       // 固定名称
		        tabClosable: true ,
				requiresTab:true
		      }
	},
	{
		path:'/ChooseCourse',
		name:'ChooseCourse',
		component:ChooseCourse,
		meta: {
		        tab: true,            // 标记为标签页
		        tabName: '在线选课',       // 固定名称
		        tabClosable: true ,
				requiresTab:true
		      }
	},
	{
		path:'/ScoreView',
		name:'ScoreView',
		component:ScoreView,
		meta: {
		        tab: true,            // 标记为标签页
		        tabName: '考试成绩',       // 固定名称
		        tabClosable: true ,
				requiresTab:true
		      }
	},
	{
		path:'/FreeRoomView',
		name:'FreeRoomView',
		component:FreeRoomView,
		meta: {
		        tab: true,            // 标记为标签页
		        tabName: '空闲教室查询',       // 固定名称
		        tabClosable: true ,
				requiresTab:true
		      }
	},
	{
		path:'/CourseView',
		name:'CourseView',
		component:CourseView,
		meta: {
		        tab: true,            // 标记为标签页
		        tabName: '课表查询',       // 固定名称
		        tabClosable: true ,
				requiresTab:true
		      }
	}
  ]
});


router.beforeEach((to, from, next) => {
  // 排除首页的特殊处理
  if (to.path !== '/HomeView' && to.meta.requiresTab) {
    const existingTab = store.state.tabs.find(t => t.path === to.fullPath)
    if (!existingTab) {
      store.commit('addTab', {
        path: to.fullPath,
        name: to.meta.tabName,
        closable: true
      })
    }
  }
  
  // 确保首页始终处于激活状态
  if (to.path === '/HomeView') {
    store.commit('setActiveTab', '/HomeView')
  }
  
  next()
})
 
export default router;